﻿using System;
using System.ComponentModel.Composition.Hosting;
using System.Diagnostics;
using ScriptEase.Shell.Controls;



namespace ScriptEase.Shell
{
    public static class ServiceLocator
    {
        public static CompositionContainer Container;


        public static T GetInstance<T>()
        {
            try
            {
                if (Container == null) return default(T); //  probably Design-Mode!
                return Container.GetExportedValue<T>();
            }
            catch (System.ComponentModel.Composition.ImportCardinalityMismatchException icme)
            {
                // No exports were found that match the constraint:                 
                throw new ApplicationException("ScriptEase cannot find its assemblies, due to an 'ImportCardinalityMismatchException'. "+
                    "This normally happens when ScriptEase was started from a location that is not fully trusted "+
                    "so that MEF does not find the required exports.\r\n\r\n"+
                    "Please run ScriptEase from a trusted location and try again!",icme);
            }
            catch (Exception e)
            {
                Trace.Default.Info(e.GetType());
                Trace.Default.LogException(e);
                throw;
            }
        }
    }


    public static class PublicFunc
    {
        public static void DisplayUnexpectedApplicationTerminationDialog(Exception ex)
        {
            ExceptionDialog2 exBox = new ExceptionDialog2(ex, "Application terminated due to an unexpected situation!\r\nPlease contact msc@schmidt-e-services.de");
            exBox.ShowDialog();
            Trace.Default.LogException(ex);
        }


        public static void RunProcess(string fileName, string arguments)
        {
            RunProcess(fileName, arguments, ProcessWindowStyle.Normal);
        }

        public static void RunProcess(string fileName, string arguments, ProcessWindowStyle style)
        {
            ProcessStartInfo ps = new ProcessStartInfo(fileName) { Arguments = arguments, WindowStyle = style };
            Process.Start(ps);
        }

    }


}